package com.zhxw.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengxiongwu on 2017/2/10.
 */
public class RecommendedFriends {

    public static class TokenizerMapper
            extends Mapper<Object, Text, Text, Text> {


        private Text word = new Text();

        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            String [] users = value.toString().split(" ");
            if(null != users && users.length == 2){
                context.write(new Text(users[0]),new Text(users[1]));
                context.write(new Text(users[1]),new Text(users[0]));
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Usage: recommended friends <in> <out>");
            System.exit(2);
        }
        Job job = new Job(conf, "recommended friends");
        job.setJarByClass(RecommendedFriends.class);
        job.setMapperClass(RecommendedFriends.TokenizerMapper.class);
        job.setCombinerClass(RecommendedReducer.class);
        job.setReducerClass(RecommendedReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
